home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / examples / nurbs / trim.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  6.8 KB  |  290 lines

  1. /*
  2.  * Copyright 1993, 1995, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* trim.c
  19.  * This program draws a rotating NURBS surface with a half-oval with
  20.  * a triangular base trimmed from its center.
  21.  *
  22.  *    Escape key    - exit the program
  23.  *    <f> key     - cycle through filled/wireframe/patch mode
  24.  */
  25. #include <GL/gl.h>
  26. #include <GL/glu.h>
  27. #include <GL/glut.h>
  28.  
  29. #include <math.h>
  30. #include <stdio.h>
  31.  
  32. /*  Function Prototypes  */
  33.  
  34. GLvoid  initgfx( GLvoid );
  35. GLvoid  drawScene( GLvoid );
  36. GLvoid  reshape( GLsizei, GLsizei );
  37. GLvoid  animate( GLvoid );
  38. GLvoid  visibility( GLint );
  39. GLvoid  keyboard( GLubyte, GLint, GLint );
  40.  
  41. GLvoid initSurface( GLvoid );
  42. GLvoid drawSurface( GLUnurbsObj * );
  43. GLvoid cycleFillMode( GLvoid );
  44.  
  45. void printNurbsError( GLenum  );
  46. void printHelp( char * );
  47.  
  48. /* Global Definitions */
  49.  
  50. #define KEY_ESC    27    /* ascii value for the escape key */
  51.  
  52. /* Global Variables */
  53.  
  54. static GLfloat         ctlpoints[4][4][3];
  55. static GLUnurbsObj    *theNurb;
  56.  
  57. static GLfloat angle = 0.0;
  58.  
  59. void
  60. main(int argc, char *argv[])
  61. {
  62.     GLsizei     width, height;
  63.  
  64.     glutInit( &argc, argv );
  65.  
  66.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  67.     height = glutGet( GLUT_SCREEN_HEIGHT );
  68.     glutInitWindowPosition( 0, height/4 ); 
  69.     glutInitWindowSize( width/2, height/2 );
  70.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
  71.     glutCreateWindow( argv[0] );
  72.     
  73.     initgfx();
  74.  
  75.     glutKeyboardFunc( keyboard );
  76.     glutReshapeFunc( reshape );
  77.     glutIdleFunc( animate ); 
  78.     glutVisibilityFunc( visibility ); 
  79.     glutDisplayFunc( drawScene ); 
  80.  
  81.     printHelp( argv[0] );
  82.  
  83.     glutMainLoop();
  84. }
  85.  
  86. void
  87. printHelp( char *progname )
  88. {
  89.     fprintf(stdout, "\n%s - draw a rotating NURBS surface "
  90.         "with a hole in the middle.\n\n"
  91.         "Escape key        - exit the program\n"
  92.         "<f> key    - cycle filled/wireframe/patch mode\n\n", 
  93.         progname);
  94.  
  95.     fprintf( stdout, "fillMode is GLU_OUTLINE_POLYGON\n" );
  96. }
  97.  
  98. /*  Initialize lighting, etc. */
  99. GLvoid
  100. initgfx( GLvoid )
  101. {
  102.     GLfloat mat_diffuse[] = { 0.8, 0.8, 0.2, 1.0 };
  103.     GLfloat mat_specular[] = { 0.8, 0.0, 0.8, 1.0 };
  104.     GLfloat mat_emission[] = { 0.0, 0.0, 0.0, 1.0 };
  105.     GLfloat mat_shininess[] = { 30.0 };
  106.  
  107.     GLfloat light_position[] = { 1.0, 0.0, 1.0, 0.0 };
  108.  
  109.     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat_diffuse);
  110.     glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
  111.     glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat_emission);
  112.     glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
  113.     glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, 1.0);
  114.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  115.  
  116.     glEnable(GL_LIGHTING);
  117.     glEnable(GL_LIGHT0);
  118.  
  119.     glClearColor( 0, 0, 0, 1 );
  120.     glEnable( GL_DEPTH_TEST );
  121.  
  122.     glEnable( GL_AUTO_NORMAL );
  123.     initSurface();
  124.     theNurb = gluNewNurbsRenderer();
  125.  
  126.     /* Set program to display the outlines of the polygons
  127.      * use to create the NURBS surface */
  128.     gluNurbsProperty( theNurb, GLU_DISPLAY_MODE, GLU_OUTLINE_POLYGON );
  129.  
  130.     /* Set up a callback function for error messages */
  131.     gluNurbsCallback( theNurb, GLU_ERROR, printNurbsError );
  132. }
  133.  
  134. GLvoid  
  135. cycleFillMode( GLvoid )
  136. {
  137.     static GLuint fillMode = GLU_OUTLINE_POLYGON;
  138.  
  139.     switch (fillMode) {
  140.     case GLU_OUTLINE_POLYGON:
  141.         fillMode = GLU_FILL;
  142.         printf("fillMode is GLU_FILL\n"); 
  143.         break;
  144.     case GLU_FILL:
  145.         fillMode = GLU_OUTLINE_PATCH;
  146.         printf("fillMode is GLU_OUTLINE_PATCH\n"); 
  147.         break;
  148.     case GLU_OUTLINE_PATCH:
  149.         fillMode = GLU_OUTLINE_POLYGON;
  150.         printf("fillMode is GLU_OUTLINE_POLYGON\n"); 
  151.         break;
  152.     }
  153.  
  154.     /* Change the fill mode */
  155.     gluNurbsProperty(theNurb, GLU_DISPLAY_MODE, fillMode);
  156. }
  157.         
  158. GLvoid 
  159. keyboard( GLubyte key, GLint x, GLint y )
  160. {
  161.     switch (key) {
  162.     case 'f':    /* cycle fill mode */
  163.         cycleFillMode();
  164.         glutPostRedisplay();
  165.         break;
  166.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  167.         exit(0);
  168.     }
  169. }
  170.  
  171. /*  Initializes the control points of the surface to a small hill.
  172.  *  The control points range from -3 to +3 in x, y, and z
  173.  */
  174. GLvoid 
  175. initSurface(void)
  176. {
  177.     int s, t;
  178.  
  179.     for (s = 0; s < 4; s++) {
  180.         for (t = 0; t < 4; t++) {
  181.             ctlpoints[s][t][0] = 2.0*((GLfloat)s - 1.5);
  182.             ctlpoints[s][t][1] = 2.0*((GLfloat)t - 1.5);
  183.  
  184.             if ( (s == 1 || s == 2) && (t == 1 || t == 2))
  185.                 ctlpoints[s][t][2] = 3.0;
  186.             else
  187.                 ctlpoints[s][t][2] = -3.0;
  188.         }
  189.     }
  190. }
  191.  
  192. /* Draw a NURBS surface with a half-oval cut out of it */
  193. GLvoid
  194. drawSurface( GLUnurbsObj *theNurb )
  195. {
  196.     GLfloat knots[8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
  197.     GLfloat edgePt[5][2] = /* counter clockwise */
  198.         {{0.0, 0.0}, {1.0, 0.0}, {1.0, 1.0}, {0.0, 1.0}, {0.0, 0.0}};
  199.  
  200.     GLfloat curveKnots[8] = 
  201.         {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
  202.     GLfloat curvePts[4][2] = /* clockwise */ 
  203.         {{0.1, 0.5}, {0.1, 0.9}, {0.9, 0.9}, {0.9, 0.5}};
  204.     GLfloat pwlPts[4][2] = /* clockwise */ 
  205.         {{0.9, 0.5}, {0.5, 0.1}, {0.1, 0.5}};
  206.  
  207.     gluBeginSurface( theNurb );
  208.         gluNurbsSurface( theNurb, 
  209.                 8, knots,
  210.                 8, knots,
  211.                 4 * 3, 3,
  212.                 &ctlpoints[0][0][0], 
  213.                 4, 4,
  214.                 GL_MAP2_VERTEX_3);
  215.  
  216.         /* counter-clockwise trimming curve outlining the surface */
  217.         gluBeginTrim( theNurb );
  218.             gluPwlCurve( theNurb, 5, &edgePt[0][0], 2, 
  219.                     GLU_MAP1_TRIM_2 );
  220.         gluEndTrim( theNurb );
  221.  
  222.         /* clockwise trimming curves for the hole */
  223.         gluBeginTrim( theNurb );
  224.             gluNurbsCurve( theNurb, 8, curveKnots, 2, 
  225.                     &curvePts[0][0], 4, GLU_MAP1_TRIM_2 );
  226.             gluPwlCurve( theNurb, 3, &pwlPts[0][0], 2, 
  227.                     GLU_MAP1_TRIM_2 );
  228.         gluEndTrim( theNurb );
  229.     gluEndSurface( theNurb );
  230. }
  231.  
  232. /* Use the NURBS error number passed in to get a meaningful
  233.  * string describing the error.
  234.  */
  235. void
  236. printNurbsError( GLenum errnum )
  237. {
  238.     printf("NURBS error: %s\n", gluErrorString( errnum ) );
  239. }
  240.  
  241. GLvoid
  242. reshape( GLsizei width, GLsizei height )
  243. {
  244.     GLdouble    aspect;
  245.  
  246.     glViewport( 0, 0, width, height );
  247.  
  248.     aspect = (GLdouble) width / (GLdouble) height;
  249.  
  250.     glMatrixMode( GL_PROJECTION );
  251.     glLoadIdentity();
  252.     gluPerspective( 45.0, aspect, 3.0, 20.0 );
  253.     glMatrixMode( GL_MODELVIEW );
  254.     glLoadIdentity();
  255.     glTranslatef( 0.0, 0.0, -15.0 ); 
  256. }
  257.  
  258. GLvoid
  259. animate( GLvoid )
  260. {
  261.     angle = fmodf( (angle + 5.0), 360.0 );    /* update angle */
  262.     glutPostRedisplay();            /* Tell GLUT to redraw */
  263. }
  264.         
  265. GLvoid
  266. visibility( int state )
  267. {
  268.     if (state == GLUT_VISIBLE) {
  269.         glutIdleFunc( animate );
  270.     } else {
  271.         glutIdleFunc( NULL );
  272.     }
  273. }
  274.  
  275. GLvoid
  276. drawScene( GLvoid )
  277. {
  278.  
  279.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  280.     glPushMatrix(); 
  281.         glRotatef( angle, 0.0, 1.0, 0.0 );
  282.  
  283.         drawSurface( theNurb );
  284.  
  285.     glPopMatrix();
  286.  
  287.     glutSwapBuffers();
  288. }
  289.  
  290.